package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alibaba.fastjson.JSON;
import com.ys.entity.base.BaseEntity;
import com.ys.entity.sys.SysCol;
import com.ys.entity.sys.SysMenu;
import com.ys.mapper.BaseMapper;
import com.ys.mapper.business.CommonMapper;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import com.ys.vo.W;

@Service("commonApiService")
public class CommonApiServiceImpl<E extends BaseEntity<E>> implements CommonApiService<E> {
	
	@Resource
	private CommonMapper commonMapper;

	@Override
	@Transactional
	public LayuiTableVoJA searchLay(E entity) {
		
		Integer page = entity.page;
		Integer size = entity.size;
		entity.setPage((page - 1) * size);

		List<Map<String, Object>> listMap = entity.queryMap();
		Integer count = entity.count();
		LayuiTableVoJA vo = new LayuiTableVoJA();
		vo.setRowsByListMap(listMap);
        vo.setCount(count);
        return vo;
	}

    @Override
    @Transactional
    public R deleteFalse(ApplicationContext ac, String className, Long id) throws Exception {
		
		@SuppressWarnings("unchecked")
		BaseMapper<E> mapperBean = (BaseMapper<E>) ac.getBean(className + "Mapper");
		mapperBean.deleteFalse(id);
        return R.ok();
    }

    @Override
    @Transactional
    public R deleteTrue(ApplicationContext ac, String className, Long id) {
		
		@SuppressWarnings("unchecked")
		BaseMapper<E> mapperBean = (BaseMapper<E>) ac.getBean(className + "Mapper");
		mapperBean.deleteTrue(id);
        return R.ok();
    }

	@Override
	public String toAdd(Model model, E entity) {
		SysMenu sysMenu = new SysMenu().setDatabaseTable(entity.generateTableAllName().replace("`", "")).queryFirst();
		
		// 查找总字段，设置MAP，方便寻找
		List<SysCol> listSysCol = new SysCol().setMenuId(sysMenu.getId()).queryList();
		Map<Long, SysCol> MAP_SysCol = new HashMap<Long, SysCol>();
		for(SysCol sysCol : listSysCol) {
			MAP_SysCol.put(sysCol.getId(), sysCol);
		}
		List<ToFormDto.RowDto> rows = new ArrayList<ToFormDto.RowDto>();

		String pathCommit = sysMenu.getPathCommitAdd();
		ToFormDto dto = new ToFormDto(null, rows, pathCommit);
		model.addAttribute("dto", dto);

		System.out.println(JSON.toJSONString(dto));
		return "sys/common/toAdd";
	}

	@Override
	public String toEdit(Model model, E entity, Long id) {
		
		if (id == null) {
			return W.error(model, "ID不能为空");
		}

		Map<String, Object> map = entity.where("A.id = " + id).queryFirstMap();
		if (map == null) {
			return W.error(model, "数据不存在");
		}

		SysMenu sysMenu = new SysMenu().setDatabaseTable(entity.generateTableAllName().replace("`", "")).queryFirst();
		
		// 查找总字段，设置MAP，方便寻找
		List<SysCol> listSysCol = new SysCol().setMenuId(sysMenu.getId()).queryList();
		Map<Long, SysCol> MAP_SysCol = new HashMap<Long, SysCol>();
		for(SysCol sysCol : listSysCol) {
			MAP_SysCol.put(sysCol.getId(), sysCol);
		}
		
		List<ToFormDto.RowDto> rows = new ArrayList<ToFormDto.RowDto>();

		String pathCommit = sysMenu.getPathCommitEdit();
		ToFormDto dto = new ToFormDto(id, rows, pathCommit);
		model.addAttribute("dto", dto);		
					
		return "sys/common/toEdit";
	}
}
